package base.thread_JUC2.chapter03.demo07;

import java.util.concurrent.Exchanger;

public class TestCase3 {
    public static void main(String[] args) {
        Exchanger<String> exchanger = new Exchanger<>();
        new Thread(new WorkA(exchanger)).start();
        new Thread(new WorkB(exchanger)).start();
    }
}


class WorkA extends Thread{
    private Exchanger<String> exchanger;

    public WorkA(Exchanger<String> exchanger) {
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        try {
            System.out.println("WorkerA想要给WorkerB" + exchanger.exchange("一个比特币"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class WorkB extends Thread{
    private Exchanger<String> exchanger;

    public WorkB(Exchanger<String> exchanger) {
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        try {
            System.out.println("WorkerB想要给WorkerA" + exchanger.exchange("1w美金"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}